#include "game.h"
#include "scene.h"
#include "creep.h"
#include <memory>
#include <vector>
#include "spawner.h"
#include <iostream>
#include "tower.h"
#include "input.h"
#include "button.h"

extern CInput OInput;

using namespace std;

CScene::CScene() {
    objekty.reserve(20000);
    buttons.reserve(1000);
    fillArray(1);
    escaped = 0;
    idcounter = 0;
    wave = 1;
    numwaves = 10;
    gameover = false;
    credits = 10;
}

CScene::~CScene() {

}

void CScene::fillArray(int n) {
    objekty.push_back(std::unique_ptr<CObject>(new CSpawner(800,100,this,1)));
}

void CScene::assignDrawer(CDrawer *what) {
    gamedrawer = what;
}

void CScene::doAll() {
    for (auto it = obj()->begin(); it != obj()->end(); it++) {
        (*it)->Do();
        if (it == obj()->end()) break;

    }
    time = SDL_GetTicks();    
    OInput.update();
}

void CScene::resetTime() {
    time = SDL_GetTicks();
}

int CScene::getTimeDiff() {
    return SDL_GetTicks() - time;
}

std::vector<std::unique_ptr<CObject> >* CScene::obj() {
    return &objekty;
}

std::vector<std::unique_ptr<CButton> >* CScene::btn() {
    return &buttons;
}

int CScene::numCreeps() {
    int n = 0;
    for (auto it = obj()->begin(); it != obj()->end(); it++) {
        if ((*it)->isCreep()) n++;
    }
    return n;
}


int CScene::getEscaped() {
    return escaped;
}

void CScene::Escape() {
    escaped++;
    credits -= 1;
}

int CScene::requestNewID() {
    return ++idcounter;
}



void CScene::delCreep(CCreep *what) {
    for (auto it = obj()->begin(); it != obj()->end(); it++) {
        if ((*it).get() == what) {
            obj()->erase(it);
            credits += 1;
            break;
        }
    }
}

int CScene::GetWave() {
    return wave;
}

int CScene::GetNumWaves() {
    return numwaves;
}

int CScene::GetCredits() {
    return credits;
}

void CScene::Credit(int what) {
    credits+=what;
}

bool CScene::gameOver() {
    return gameover;
}

void CScene::setGameOver() {
    gameover = true;
}

void CScene::NextWave() {
    if (numCreeps() == 0) {
        wave++;
        int tshp = wave;
        if (wave % 5 == 0) tshp *= 2;
        gamespawner->ReFill(10+wave,tshp);
    }
}

void CScene::assignSpawner(CSpawner *what) {
    gamespawner = what;
}

int CScene::GetRegistryValue(string temp) {
    return registry[temp];
}

void CScene::SetRegistryValue(string temp, int what) {
    registry[temp] = what;
}
